


//主要是判定系统，比如时间系统，战斗系统，数据管理事件触发系统。

package Engine;

import Creature.Food;
import Creature.Player;
import Event.EventLine;
import Event.MainStory;
import Event.Story;
import Event.daliy.ChooseEating;
import Interface.MyInterface;
import Interface.Play0;
import Item.Item;

import javax.swing.*;
import java.awt.event.*;
import java.net.URL;

public class System1 implements ActionListener {
    int days;
    Player player;
    MyInterface myInterface;
    public Story curEvent;
    EventLine eventLine;
    int returnNum;
    int choose = 0;
    boolean nextDay = true;
    boolean nextEvent = false;
    JButton nextButton = new JButton();
    GameSave gameSave;
    GameLoader gameLoader;

    System1(MyInterface mainInterface) {
        nextButton.setBounds(650, 550, 150, 250);
        nextButton.addActionListener(this);
        myInterface = mainInterface;
        player = new Player();
        eventLine = new EventLine(player);
        days = 0;
        gameLoader = new GameLoader(this);
        gameSave = new GameSave(this);
    }

    public void showDay() {
        myInterface.setDayLabel(days);
    }


    public void showStatus() {
        String status = "你的状态：";
        if (player.isHungry()) {
            status += "饥饿、";
        }
        if (player.isSick()) {
            status += player.sickType() + "、";
        }
        if (player.isFatigue()) {
            status += "疲劳、";
        }
        if (player.getHealth() > 60) {
            status += "健康";
        }

        myInterface.setTopLabel(status + "      money:" + player.getMoney());
    }

    public void showFood() {
        StringBuilder message = new StringBuilder("食物：\n");
        for (Food food : player.foods) {
            message.append(food.getName()).append("  ").append("\n");
        }
        myInterface.setFood(message.toString());
    }

    public void showItem() {
        StringBuilder message = new StringBuilder("道具：\n");
        for (Item item : player.itemList) {
            message.append(item.getName()).append("\n");

        }
        myInterface.setItem(message.toString());
    }

    //重新绘制界面
    public void refresh() {
        showFood();
        showItem();
        showStatus();
        showDay();
        myInterface.repaint();
        myInterface.validate();
        myInterface.invalidate();
        myInterface.validate();
    }

    //主线
    public void mainLoop() throws InterruptedException {
        gameLoader.loadGame();
        myInterface.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                gameSave.save();
                System.exit(0);

            }
        });
        while (true) {
            if (nextDay) {
                nextDay = false;
                player.setHungry(player.getHungry()-10);
                days++;
                curEvent = eventLine.pushEvent(days);
                doEvent();
                if (eventLine.keyEventIndex < 4) {
                    //设置第一个事件
                    curEvent = eventLine.pushDailyEvent1();
                    //执行第一个事件
                    doEvent();
                    //设置第二个事件
                    curEvent = eventLine.pushDailyEvent2();
                    doEvent();
                }
                curEvent = new ChooseEating(player);
                doEvent();
                //展示下一天的按钮
                showButton();
                refresh();
            } else {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }
    }

    public void showButton() {
        nextButton.setText("下一天");
        myInterface.evenText.setVisible(false);
        myInterface.buttonPanel.setVisible(false);
        myInterface.add(nextButton);
        nextButton.requestFocus();
    }

    //显示下一个事件的按钮
    public void showNextButton() {
        nextButton.setText("下一事件");
        myInterface.add(nextButton);
        //等待按钮被点击
        nextButton.requestFocus();
    }

    //设置事件界面
    public void doEvent() throws InterruptedException {
        //设置事件描述文本
        if (curEvent instanceof MainStory){
            MainStory main=(MainStory) curEvent;
            if(main.header!=null){
                myInterface.setBackground(main.header);
            }
        }

        myInterface.setEvenText(curEvent.happenText);
        JButton[] buttonList = myInterface.showButton(curEvent.options);

        //给所有的按键添加响应
        for (JButton button : buttonList) {
            button.addActionListener(this);
        }
        //更新界面
        refresh();
        //等待玩家点击
        while (choose == 0) {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        choose = 0;
        //选择了选项后执行对应事件
        curEvent.eventHappen(returnNum);

        //显示结局
        myInterface.setEvenText(curEvent.endText);

        //显示下一个按钮
        showNextButton();

        //移除按钮面板
        myInterface.buttonPanel.removeAll();

        //更新界面
        refresh();

        while (true) {
            if (nextEvent) {
                break;
            } else {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        nextEvent = false;

        if (curEvent.child != null) {
            curEvent = curEvent.child;
            doEvent();
        }
        if (curEvent instanceof MainStory) {
            MainStory mainStory = (MainStory) curEvent;
            eventLine.keyEventIndex = mainStory.nextEventIndex;
        }
    }


    //点击的按键监听
    @Override
    public void actionPerformed(ActionEvent e) {

        //如果点击了下一天就把按钮面板。事件文本显示出来，再移除下一天按钮，更新一天
        if (e.getActionCommand().equals("下一天")) {
            myInterface.buttonPanel.setVisible(true);
            myInterface.evenText.setVisible(true);
            myInterface.remove(nextButton);
            nextDay = true;
        } else if (e.getActionCommand().equals("下一事件")) {
            myInterface.remove(nextButton);
            nextEvent = true;
            choose = 0;
        } else if (e.getActionCommand().equals("结束游戏")) {
            gameLoader.initGame();
            gameSave.save();
            System.exit(0);
        }
        else {
            //如果choose等于1,根据事件选项长度来返回按钮应该设置的数量
            choose = 1;
            new Play0("src/Interface/点击音效.mp3").start();
            for (int j = 0; j < curEvent.options.length; j++) {
                if (curEvent.options[j].equals(e.getActionCommand())) {
                    returnNum = j;
                    break;
                }
            }
        }
    }
}

